package com.tongfusoft.document.web.controller.administration;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.AccountHasUsers;
import com.tongfusoft.document.entity.Accounts;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.account.service.IDocumentAccountService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.DataTable;
import com.tongfusoft.spring.web.core.data.ResultModel;

@Controller
public class AccountController extends BaseController {

	@Autowired
	@Qualifier(value = "documentAccountService")
	public IDocumentAccountService accountService;
	
	@UrlDesc(value = "账户管理页")
	@RequestMapping("/manager/account")
	public String account(HttpServletRequest request, Model model) {

		model.addAttribute("mainPageKey", "account");
		model.addAttribute("mainPage", "manager/account/account");

		return "base";
	}

	@UrlDesc(value = "所有账户分页")
	@RequestMapping("/account/query")
	@ResponseBody
	public DataTable<Accounts> groupQuery(Model model, int length, int start,
			int draw, String groupCode, String order) {
		DataTable rm = new DataTable();
		try {
			Accounts account = new Accounts();
			int pageIndex = (start / length);
			PageInfo pageInfo = this.accountService.accountPageTable(pageIndex,
					length, account);
			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rm;
	}

	@UrlDesc(value = "所有账户")
	@RequestMapping("/accountList/query")
	@ResponseBody
	public ResultModel accountListQuery(HttpServletRequest request, Users user,
			Model model) {
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			List<Accounts> list = this.accountService.accountListById(user);
			rm.setData(list);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rm;
	}

	@UrlDesc(value = "新增账户")
	@RequestMapping("/account/add")
	@ResponseBody
	public ResultModel accountAdd(Accounts account) {
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {

			this.accountService.createAccount(account, (Users) super.getUser());
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;
	}

	@UrlDesc(value = "用户与账户绑定与否")
	@RequestMapping("/isAccountHasUsers")
	@ResponseBody
	public ResultModel accountHasUsersGet(AccountHasUsers accountHasUsers,
			Model model) {

		ResultModel rm = new ResultModel();
		rm.setState("success");

		try {
			List<AccountHasUsers> list = this.accountService
					.getAccountHasUser(accountHasUsers);
			rm.setData(list);
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}
		return rm;
	}

	@UrlDesc(value = "删除账户")
	@RequestMapping("/account/del")
	@ResponseBody
	public ResultModel accountDel(Accounts account) {
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {

			this.accountService.deleteAccount(account, (Users) super.getUser());
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;
	}

	@UrlDesc(value = "账户详情")
	@RequestMapping("/account/detail")
	@ResponseBody
	public ResultModel accountDetail(Accounts account) {
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {

			rm.setData(this.accountService.detailAccount(account));
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;
	}

	@UrlDesc(value = "编辑账户")
	@RequestMapping("/account/update")
	@ResponseBody
	public ResultModel accountUpdate(Accounts account) {
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {

			this.accountService.updateAccount(account, (Users) super.getUser());
		} catch (ServiceException se) {
			rm.setCode(se.getMessage());
			rm.setState("error");
		} catch (Exception e) {
			rm.setState("error");
			e.printStackTrace();
		}

		return rm;
	}
}
